﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Oracle;

namespace VirtualAccess.Devices.Oracle.Enterprise
{
    public class SelectUpdateCommand
    {
        private DbCommand innerCommand = null;

        public object Execute()
        {
            PrepareExecute();

            //取得数据库对象。
            OracleDatabase database = DataProvider.Database;
            //设置参数值。
            FieldValueCollection fieldValues = this.FieldValues;
            FieldValue fieldValue;
            int count = fieldValues.Count;
            for (int i = 0; i < count; i++)
            {
                fieldValue = fieldValues[i];
                database.SetParameterValue(innerCommand, fieldValue.ParameterName, fieldValue.FieldValue);
            }

            //执行命令。
            return Database.ExecuteNonQuery(innerCommand);
        }

        private void PrepareExecute()
        {
            if (innerCommand == null)
            {
                //取得命令配置。
                OracleCommandConfigration configration = GetCommandConfigration();
                //取得数据库对象。
                OracleDatabase database = DataProvider.Database;
                //创建命令。
                innerCommand = database.GetSqlStringCommand(configration.Sql);
                //准备参数。
                FieldValueCollection fieldValues = this.FieldValues;
                FieldValue fieldValue;
                int count = fieldValues.Count;
                for (int i = 0; i < count; i++)
                {
                    fieldValue = fieldValues[i];
                    switch (fieldValue.DataType)
                    {
                        case DataType.String:
                            if (fieldValue.Length > 4000)
                            {
                                database.AddParameter(dbCommand, fieldValue.FieldName,
                                    OracleType.Clob, fieldValue.Length,
                                    ParameterDirection.Input, false, 0, 0, string.Empty,
                                    DataRowVersion.Default, null);
                            }
                            else
                            {
                                database.AddInParameter(innerCommand, fieldValue.FieldName,
                                    DbType.String, null);
                            }
                            break;
                        case DataType.Decimal:
                            database.AddInParameter(dbCommand, fieldValue.FieldName, DbType.Decimal, null);
                            break;
                        case DataType.DateTime:
                            database.AddInParameter(dbCommand, fieldValue.FieldName, DbType.DateTime, null);
                            break;
                        case DataType.Binary:
                            database.AddParameter(cmd, fieldValue.FieldName,
                                OracleType.Blob, fieldValue.Length,
                                ParameterDirection.Input, false, 0, 0, string.Empty,
                                DataRowVersion.Default, value);
                            break;
                    }
                }
            }
        }
    }
}
